

## CODE for replication of Can conservatism make women more vulnerable to violence?
## Authors: Victor Araujo (UZH) Malu Gatto (UCL)
## Journal: Comparative Political Studies (CPS)
## BEFORE running this code, please see the "READ_ME_FIRST" file for general instructions. 
  # All procedures and analysis were executed using R version 4.0.3 (2020-10-10)



### Code for replication of Figure 1 (Main text)

library(sf)
library(geobr)
library(tidyverse)


### Loading data for the whole country (Brazil). This might take a few seconds.
all_muni_10 <- read_municipality(year= 2010)
all_muni_10$code_muni <- factor(all_muni_10$code_muni)


## Opening data from 2018 MUNIC
data_wpm <- read.csv("data_wpm.csv", sep =",", header = TRUE)
data_wpm <- select(data_wpm, Cod.Municipio, any_policy, any_justice, any_protection, MPPM13, MPPM241,
                   MPPM242, MPPM243, MPPM244, MPPM245, MPPM246, MPPM247,MPPM249, MPPM2410, sum_all, justice_all, protection_all)
data_wpm <- rename(data_wpm, code_muni = Cod.Municipio)
data_wpm$code_muni <- factor(data_wpm$code_muni)


data_mapa <- all_muni_10 %>% 
  left_join(data_wpm, by = "code_muni")


### Color figure
p1 <- ggplot() +
  geom_sf(data=data_mapa, aes(fill=sum_all), color= NA, size=.10) +
  labs(subtitle="", size=10) +
  scale_fill_distiller(palette = "Paired", direction = 1,  name="Number of policy instruments", limits = c(0,10)) +
  theme_minimal() 
p1_2 <- p1 + theme(legend.position="bottom")
p1_2 



## Black and White figure
p1 <- ggplot() +
  geom_sf(data=data_mapa, aes(fill=sum_all), color= NA, size=.10) +
  labs(subtitle="", size=10) +
  scale_fill_distiller(palette = "Greys", direction = 1,  name="Number of policy instruments", limits = c(0,10)) +
  theme_minimal() 
p1_2 <- p1 + theme(legend.position="bottom")
p1_2 



##########################################################
## Code for the creation of munic_analysis.dta dataset
#########################################################

## Required packages

library(tidyverse)
library(haven)
library(foreign)


# FIRST STEP: setting the directory in your computer


setwd("G:/My Drive/Ara?jo_Gatto_papers/mulheres_MUNIC/Policies to tackle Violence against Women/data/CPS_materials")

data_wpm <- read.csv("data_wpm.csv", sep =",", header = TRUE)
data_wpm <- select(data_wpm, Cod.Municipio, any_policy, any_justice, any_protection, MPPM13, MPPM241,
                   MPPM242, MPPM243, MPPM244, MPPM245, MPPM246, MPPM247, MPPM248, MPPM249, MPPM2410, sum_all, justice_all, protection_all)

ibge6 <- as.numeric(substr(data_wpm$Cod.Municipio, 1, 6))
data_wpm <- data.frame(data_wpm, ibge6)
data_wpm$ibge6 <- factor(data_wpm$ibge6)



data_wpm2 <- read.csv("data_wpm2.csv", sep =",", header = TRUE)
data_wpm2 <- select(data_wpm2,Cod.Municipio,MTIP03,MTIP05,MTIP07,MTIP09,MTIP11,MTIP13)

ibge6 <- as.numeric(substr(data_wpm2$Cod.Municipio, 1, 6))
data_wpm2 <- data.frame(data_wpm2, ibge6)
data_wpm2$ibge6 <- factor(data_wpm2$ibge6)



## Opening data from 2010 Brazilian census
censo_10 <- read.csv("data_censo_10.csv", sep =",", header = TRUE)
censo_10 <- select(censo_10, codmun6, codmun7, municpio, uf, region, pesotot, mulhertot, fectot, e_anosestudo, pesorur, pesourb, gini, 
                   idhm, pmpob, t_des18m, p_formal, popt)

censo_10$prop_urb <- NA
censo_10$prop_urb <- censo_10$pesourb/censo_10$pesotot
censo_10$prop_mulher <- NA
censo_10$prop_mulher <- censo_10$mulhertot/censo_10$pesotot
censo_10 <- select(censo_10, codmun6, codmun7, municpio, uf, region, pesotot, fectot, e_anosestudo, gini, 
                   idhm, pmpob, t_des18m, p_formal, prop_urb, prop_mulher, popt)

ibge6 <- as.numeric(substr(censo_10$codmun7, 1, 6))
censo_10 <- data.frame(censo_10, ibge6)
censo_10$ibge6 <- factor(censo_10$ibge6)




## Opening data from 2010 Brazilian census (religion labels)
religion_10 <- read.csv("religion_labels_censo.csv", sep =",", header = TRUE)
religion_10$prop_trad <- NA
religion_10$prop_trad <- religion_10$trad_10/religion_10$total_10
religion_10$prop_pent <- NA
religion_10$prop_pent <- religion_10$pent_10/religion_10$total_10
religion_10$prop_catol <- NA
religion_10$prop_catol <- religion_10$cato_10/religion_10$total_10
religion_10 <- select(religion_10, IBGE_7, prop_trad, prop_pent, prop_catol)
ibge6 <- as.numeric(substr(religion_10$IBGE_7, 1, 6))
religion_10 <- data.frame(religion_10, ibge6)
religion_10$ibge6 <- factor(religion_10$ibge6)



## Opening data on women homicides rates from Mapa da Violencia (2015)
data_homiw <- read.csv("homicides_rates.csv", sep =",", header = TRUE)
data_homiw$munic <- factor(data_homiw$munic)
data_homiw$uf <- factor(data_homiw$uf)
data_nomes <- read.csv("nomes_munic.csv", sep =",", header = TRUE)
data_nomes$munic <- factor(data_nomes$munic)
data_nomes$uf <- factor(data_nomes$uf)
merge_homicides <- left_join(data_homiw, data_nomes, by = c("munic", "uf"))
merge_homicides <- select(merge_homicides, ibge6, ibge7, municipio, uf, code_uf, taxa_media)
merge_homicides$ibge6 <- factor(merge_homicides$ibge6)
merge_homicides <- rename(merge_homicides, whr_average = taxa_media)



### Opening data on municipal ideological scores from Power and Silveira (2019)
data_ideo <- read_dta("ideology.dta")
ibge6 <- as.numeric(substr(data_ideo$ibge7, 1, 6))
data_ideo <- data.frame(data_ideo, ibge6)
data_ideo$ibge6 <- factor(data_ideo$ibge6)
data_ideo <- select(data_ideo, ibge6, ideo_na_2016)



### Opening data on municipal state capacity measured by the proportion of employees with high education

data_high_educ <- read.csv("high_educ.csv", sep =",", header = TRUE)
ibge6 <- as.numeric(substr(data_high_educ$ibge7, 1, 6))
data_high_educ <- data.frame(data_high_educ, ibge6)
data_high_educ$ibge6 <- factor(data_high_educ$ibge6)
data_high_educ <- select(data_high_educ, ibge6, n_high_educ)



### Opening data on profile of candidates elected to municipal councils from TSE (2004-2016)
data_ver <- read_dta("data_vereador.dta")
ibge6 <- as.numeric(substr(data_ver$COD_MUN_IBGE, 1, 6))
data_ver <- data.frame(data_ver, ibge6)
data_ver$ibge6 <- factor(data_ver$ibge6)



### Opening data on women elected mayor from TSE (2004-2016)
data_women <- read_dta("women_elected.dta")
ibge6 <- as.numeric(substr(data_women$ibge7, 1, 6))
data_women <- data.frame(data_women, ibge6)
data_women$ibge6 <- factor(data_women$ibge6)
data_women <- select(data_women, ibge6, woman_ever, woman_sum,prop_woman)



## Opening data on pastors elected to municipal councils from TSE (2004-2016)
data_pastorver <- read_dta("data_pastor.dta")
ibge6 <- as.numeric(substr(data_pastorver$COD_MUN_IBGE, 1, 6))
data_pastorver <- data.frame(data_pastorver,ibge6)
data_pastorver$ibge6 <- factor(data_pastorver$ibge6)



## Opening data on mayors' characteristics (ideology + schooling) from TSE (2004-2016)
data_prefe <- read.csv("data_pref_educ_right.csv", sep =",", header = TRUE)
ibge6 <- as.numeric(substr(data_prefe$COD_MUN_IBGE, 1, 6))
data_prefe <- data.frame(data_prefe,ibge6)
data_prefe$ibge6 <- factor(data_prefe$ibge6)



## Merging datasets to create the munic_analysis.dta dataset

merged_data <- data_wpm2 %>%
  left_join(data_wpm, by = "ibge6") %>%
  left_join(censo_10, by = "ibge6") %>%
  left_join(religion_10, by = "ibge6") %>%
  left_join(merge_homicides, by = "ibge6") %>%
  left_join(data_ideo, by = "ibge6") %>%
  left_join(data_high_educ, by = "ibge6") %>%
  left_join(data_ver, by = "ibge6") %>%
  left_join(data_women, by = "ibge6") %>%
  left_join(data_pastorver, by = "ibge6") %>%
  left_join(data_prefe, by = "ibge6")




#######################################################################
##### Calculating the the number per capita of high educated employees
#######################################################################

merged_data$high_educ_pc <- NA
merged_data$high_educ_pc <- merged_data$n_high_educ/merged_data$pesotot



merged_data <- select(merged_data, Cod.Municipio.x , municipio, code_uf, uf.y, 
                      ideo_na_2016, whr_average, prop_mulher, prop_urb,popt, prop_trad,high_educ_pc, 
                      prop_pent, prop_catol,idhm,prop_woman, mean_prop_rightpref, mean_prop_mulher, 
                      mean_prop_right, mean_prop_pastor,MTIP05,MTIP07,MTIP09,MTIP11,MTIP13,
                      MPPM13, MPPM241, MPPM242, MPPM243, MPPM244, MPPM245, 
                      MPPM246, MPPM247,MPPM249, MPPM2410,sum_all,justice_all, protection_all,
                      any_policy, any_justice, any_protection) 
                      
                      
                
                    
write.dta(merged_data,file=paste("munic_analysis.dta"))







